package com.mi.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.mi.bean.Manager;
import com.mi.bean.SysdbapswBean;
import com.mi.service.impl.SysPswImpl;
import com.mi.utils.MyUtils;

/**
 * Servlet implementation class SysdbaAction
 */
@WebServlet("/SysdbaAction")
public class SysdbaAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 业务逻辑处理
		// 设置request编码
		// request.setCharacterEncoding("utf-8");

		// 取出客户端发送的表单数据
		// 将表单数据存入实体bean中进行传输
		SysdbapswBean pswB = MyUtils.convertFormData2Bean(request, SysdbapswBean.class);
	
		MyUtils.logRootLogger.info("管理员名：" + pswB.getSysdbaname());
		MyUtils.logRootLogger.info("密码：" +pswB.getDbapsw());
		// 调用model层进行业务逻辑处理：查询数据库进行用户信息合法性验证
		List<SysdbapswBean> lstPsw = MyUtils.getInstance(SysPswImpl.class).queryLoginInfo(pswB);
		// 根据接收到的model的返回结果，进行用户视图的转发控制
		/*if (lstPsw != null && lstPsw.size() > 0) {
			// 登录成功，跳转到登录成功的页面
			// 缓存登录成功的用户信息
			request.getSession().setAttribute("dbapsw", lstPsw.get(0));
			//response.setCharacterEncoding("utf-8");
			//使用转发
			request.getRequestDispatcher("WEB-INF/logined/adminManage/ManagerRegisterUpload.jsp").forward(request, response);
			// 使用重定向
			//response.send Redirect("logined/main.jsp");
		} else {
			// 登录失败，重新登录
			// 使用转发
			request.getRequestDispatcher("WEB-INF/logined/adminManage/PswInputFailJump.jsp").forward(request, response);
		}*/
		
		if (lstPsw != null && lstPsw.size() > 0) {
			// 登录成功，跳转到登录成功的页面
			// 缓存登录成功的用户信息
			request.getSession().setAttribute("dbapsw", lstPsw.get(0));
			//response.setCharacterEncoding("utf-8");

			String strJson=JSON.toJSONStringWithDateFormat(lstPsw, "yyyy-MM-dd HH:mm:ss");
			//ajax响应
			//响应客户端的请求
			PrintWriter pw=response.getWriter();
			pw.write(strJson);
			pw.flush();
			pw.close();
			
		}
		
	}

}
